`
https://leetcode.cn/problems/house-robber-ii/
`

/**
 * @param {number[]} nums
 * @return {number}
 */
var rob = function (nums) {
  let n = nums.length;
  if (n === 1) return nums[0];
  // 要么第一间抢最后一间不抢，要么最后一间不抢第一间抢
  return Math.max(robRange(nums, 0, n - 2), robRange(nums, 1, n - 1));
};

// https://leetcode.cn/problems/house-robber/
var robRange = function (nums, start, end) {
  // 记录 dp[i+1] 和 dp[i+2]
  let dp_i_1 = 0, dp_i_2 = 0;
  // 记录 dp[i]
  let dp_i = 0;
  for (let i = end; i >= start; i--) {
    dp_i = Math.max(dp_i_1, nums[i] + dp_i_2);
    dp_i_2 = dp_i_1;
    dp_i_1 = dp_i;
  }
  return dp_i;
};